def get_model_collection_sets(model_class):
    coll_set = []
    for cls in model_class.__mro__:
        if hasattr(cls, '_collection_sets'):
            coll_set.extend(getattr(cls, '_collection_sets'))
    return list(set(coll_set))

def get_model_collections_doc(model_class, indent=''):
    coll_str = "%s%s\n" % (indent, model_class.__name__)
    if hasattr(model_class, '_collection_sets'):
        for collection in get_model_collection_sets(model_class):
            coll_str += get_model_collections_doc(
                getattr(model_class, collection).related.model,
                indent + '  ')
    return coll_str

def collections_doc():
    coll_str = ''
    from models import root_elements
    for mdl in root_elements:
        coll_str += get_model_collections_doc(mdl)
    return coll_str

def collections_list():
    def model_collections_list(model_class):
        coll_list = [model_class]
        if hasattr(model_class, '_collection_sets'):
            for collection in get_model_collection_sets(model_class):
                coll_list.extend(model_collections_list(
                    getattr(model_class, collection).related.model))
        return coll_list

    collections = []
    from models import root_elements
    for mdl in root_elements:
        collections.extend(model_collections_list(mdl))
    return list(set(collections))
